package com.hsd.whb.utils;

import com.hsd.whb.enums.CodeEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EnumUtil {

    public static <T extends CodeEnum> T getEnumByCode(Integer code, Class<T> enumClass) {
        for (T each : enumClass.getEnumConstants()) {
            if (code.equals(each.getCode())) {
                return each;
            }
        }
        return null;
    }

    public static <T extends CodeEnum> String getStringByCode(Integer code, Class<T> enumClass) {
        T t = getEnumByCode(code, enumClass);
        if (t != null) return t.getMessage();
        return null;
    }

    public static <T extends CodeEnum> List<Map<String, Object>> getEnumInfo(Class<T> enumClass, String code) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (T each : enumClass.getEnumConstants()) {
            Map<String, Object> map = new HashMap<>();
            map.put(code, each.getCode());
            map.put("typeName", each.getMessage());
            list.add(map);
        }
        return list;
    }

    public static <T extends CodeEnum> List<Map<String, Object>> getEnumInfo(Class<T> enumClass, String code, String message) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (T each : enumClass.getEnumConstants()) {
            Map<String, Object> map = new HashMap<>();
            map.put(code, each.getCode());
            map.put(message, each.getMessage());
            list.add(map);
        }
        return list;
    }
}
